<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRole"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{row}">
              <el-button size="small">分配权限</el-button>
              <el-button size="small">编辑</el-button>
              <el-button size="small" type="danger" @click="fetchDelRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination layout="prev,pager,next" :total="total" :page-size="params.pagesize"
          @current-change="changePage" />
        </el-row>
      </el-card>
    </div>
     <!-- 新增弹框 -->
    <el-dialog title="编辑弹层" :visible="roleDialogVisible" @close="closeDialog">
      <!-- 表单内容 -->
      <template #default>
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
      </template>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="closeRoleDialog">取消</el-button>
        <el-button size="small" type="primary" @click="confirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  import { getRoleList, deleteRole, addRole } from '@/api/setting'
  export default {
    data() {
      return {
        roleList: [],
        params: {
            page: 2,
            pagesize: 4
        },
        total: 0,
        roleDialogVisible: false,
        roleForm: {
          name: '',
          description: ''
        },
        rules: {
          name: [
            { required: true, message: '角色名称必填', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '角色描述必填', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.fetchList() // 获取角色列表
    },
    methods: {
      async fetchList() {
        const res = await getRoleList(this.params)
        this.roleList = res.rows
        this.total = res.total
      },
      changePage(page) {
      // newPage是当前点击的页码
        this.params.page = page // 将当前页码赋值给当前的最新页码
        this.fetchList()
      },
       // 删除
      fetchDelRole(id) {
        this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          // 调用删除接口
          await deleteRole(id)
          // 更新列表
          this.fetchList()
          // 提示用户
          this.$message.success('删除角色成功')
          }).catch()
        },
        addRole() {
        this.roleDialogVisible = true
      },
      closeDialog() {
        this.roleDialogVisible = false
      },
      confirm() {
        this.$refs.roleForm.validate(async(valid) => {
          if (valid) {
          // 接口
            await addRole(this.roleForm)
            // 更新列表
            this.fetchList()
            // 提示用户
            this.$message.success('新增成功')
            // 关闭弹框
            this.roleDialogVisible = false
            // 重置数据
            this.$refs.roleForm.resetFields() // 本身它有清除数据和清除校验痕迹的作用
            this.roleForm = {
              name: '',
              description: ''
            }
          }
        })
      },
      closeRoleDialog () {
        // 清空表单数据
        this.roleForm = {
          name: '',
          description: ''
        }
        // 移除校验
        this.$refs.roleForm.resetFields()
        // 关闭弹框
        this.roleDialogVisible = false
      }
    }
  }
</script>